<?php

namespace app\admin\model;

use think\Exception;
use think\Model;

class Fake extends Model
{
    public function good()
    {
        return $this->hasOne(Goods::class , 'id' , 'good_id');
    }
    /**
     * @param int $type 卡密类型 0添加积分卡密，1添加会员时长卡密，2积分奖品兑换卡密
     * @param Array $param = ['num' => 1 , 'type' => 1 ,'score' => 0 ,'vip_time' => 0];
     */
    public static function add($param = [])
    {
        $num = array_key_exists('addNum',$param) ? $param['addNum'] : 1;
        $param['num'] = empty(@$param['score']) ? $param['vip_num'] : $param['score'];
        unset($param['addNum'],$param['score'],$param['vip_num']);
        for ($i = 0; $i < $num; $i++) {
            $param['fake'] = randStr(20);
            self::create($param);
        }
    }
    public static function getFake($param = [])
    {
        $fakeList = '';
        $param['status'] = 0;
        if(!array_key_exists('good_id',$param)){
            $param['num'] = empty(@$param['score']) ? $param['vip_num'] : $param['score'];
        }
        $needNum = @$param['addNum'] ?? 1;
        $j = 0;
        unset($param['addNum'],$param['score'],$param['vip_num']);
        $list = self::where($param)->select();
        foreach ($list as $k => $v) {
            if ($needNum != $j) {
                // 更新卡密为提取状态 2 
                self::where('id',$v['id'])->update(['status' => 2]);
                $fakeList .= $v['fake'] . "<br>";
                $j++;
            }
        }
        return $fakeList;
    }
    public static function vip_timeStamp($type, $num)
    {
        switch ($type) {
            case '1':
                $time = 24 * 3600;
                break;
            case '2':
                $time = 24 * 3600 * 30;
                break;
            case '3':
                $time = 24 * 3600 * 365;
                break;
        }
        return $time * $num;
    }
}
